//
//  ViewController.m
//  CollectionTest
//
//  Created by A on 2020/11/7.
//  Copyright © 2020 TyTeam. All rights reserved.
//

#import "ViewController.h"
#import <ZhuoZhuo/ZhuoZhuo.h>
#import "CustomLayout.h"
#import "CustomCollectionCell.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,CustomLaoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    [self initDataSource];

    [self initUI];
    
    
}


- (void)initDataSource {
    
    self.dataArr = [NSMutableArray array];
 
}

- (void)initUI {
    
    CustomLayout *layout = [[CustomLayout alloc] init];
    layout.delegate = self;
    
    self.collectionV = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor yellowColor];
    self.collectionV.dataSource = self;
    self.collectionV.delegate = self;
    [self.collectionV registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:@"homeCustomCell"];
    
    [self.view addSubview:self.collectionV];
    
    [self getCellData];
    
}

//获取数据
- (void)getCellData {
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSArray *array = GetListData__NotAllowInMainThread();
        self.dataArr = [NSMutableArray arrayWithArray:array];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionV reloadData];
        });
     
    });
}


- (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}


#pragma mark === CustomLaoutDelegate
- (CGFloat)getCellHeightForItemAtIndex:(NSInteger)index itemWidth:(CGFloat)itemWidth {
    
    CGFloat mainImg_height = 0;
    CGFloat mainLab_height = 0;
    CGFloat secondLab_height = 0;
    CGFloat headImg_height = 40;
    CGFloat totalHeigt = 0;
    
    RDMTCellData *data = self.dataArr[index];
    
    //计算高度
    switch (data.DataType) {
        case 1:
        {
            ImageData *imgData = data.Image;
            mainImg_height = itemWidth * (imgData.ImageHeight/imgData.ImageWidth);
            
            totalHeigt = mainImg_height+10+headImg_height+10;
            
            return totalHeigt;
        }
            break;
        case 2:
        {
            if (![data.TitleText isEqualToString:@""]) {
                mainLab_height = [self getHeightByWidth:itemWidth title:data.TitleText font:[UIFont systemFontOfSize:15]];
            }

            if (![data.SecondTitleText isEqualToString:@""]) {
                secondLab_height = [self getHeightByWidth:itemWidth title:data.SecondTitleText font:[UIFont systemFontOfSize:15]];
            }
            
            totalHeigt = 10+mainLab_height+10+secondLab_height+10;
            
            return totalHeigt;
        }
            break;
        case 3:
        {
            if (data.Image) {
                ImageData *imgData = data.Image;
                mainImg_height = itemWidth * (imgData.ImageHeight/imgData.ImageWidth);
            }

            if (![data.TitleText isEqualToString:@""]) {
                mainLab_height = [self getHeightByWidth:itemWidth title:data.TitleText font:[UIFont systemFontOfSize:15]];
            }

            if (![data.SecondTitleText isEqualToString:@""]) {
                secondLab_height = [self getHeightByWidth:itemWidth title:data.SecondTitleText font:[UIFont systemFontOfSize:15]];
            }
            
            totalHeigt = mainImg_height+10+mainLab_height+10+secondLab_height+10+headImg_height+10;
            
            return totalHeigt;
        }
            break;
            
        default:
            break;
    }


    return totalHeigt;
}

#pragma mark === UICollectionViewDataSource,UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"homeCustomCell" forIndexPath:indexPath];
    RDMTCellData *data = self.dataArr[indexPath.row];
    
    [cell buildCellWithData:data];
    
    return cell;
}

@end
